package com.share.sharedevice.service.impl;

import com.share.sharedevice.entity.Cabinet;
import com.share.sharedevice.entity.StationLocation;
import com.share.sharedevice.repository.StationLocationRepository;
import com.share.sharedevice.service.ICabinetService;
import com.share.sharedevice.service.IDeviceService;
import com.share.sharedevice.service.IStationService;
import com.share.sharedevice.vo.StationNewVo;
import com.share.sharedevice.vo.StationVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Circle;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.Metrics;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;
//
@Slf4j
@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class DeviceServiceImpl implements IDeviceService {


    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private StationLocationRepository stationLocationRepository;

    @Autowired
    private IStationService stationService;

    @Autowired
    private ICabinetService cabinetService;

    /**
     * 附近的站点
     *
     * @param latitude  纬度
     * @param longitude 经度
     * @return
     */

    @Override
    public List<StationNewVo> nearbyStation(String latitude, String longitude) {

        //确定中心点
        //传入经纬度转换string为Double类型
        GeoJsonPoint geoJsonPoint = new GeoJsonPoint(Double.parseDouble(longitude), Double.parseDouble(latitude));

        //设置查询半径(5,KM)
        Distance distance = new Distance(5, Metrics.KILOMETERS);

        //画圆
        //（第一个是中心点，第二个是半径）
        Circle circle = new Circle(geoJsonPoint, distance);

        //查询DB数据

        Query location = Query.query(Criteria.where("location").within(circle));

        List<StationLocation> list = mongoTemplate.find(location, StationLocation.class);

        //查询其他数据进行封装

        List<StationVo> collect = list.stream().map(i -> {
            //获取每一个站点ID
            Long stationId = i.getStationId();

            StationVo byStationId = stationService.getByStationId(stationId);

            return byStationId;
        }).collect(Collectors.toList());

        List<StationNewVo> collect1 = collect.stream().map(i -> {
            StationNewVo stationNewVo = new StationNewVo();
            stationNewVo.setId(i.getId());
            stationNewVo.setName(i.getName());
            stationNewVo.setImageUrl(i.getImageUrl());
            stationNewVo.setBusinessHours(i.getBusinessHours());
            stationNewVo.setLongitude(i.getLongitude());
            stationNewVo.setLatitude(i.getLatitude());
            stationNewVo.setFullAddress(i.getFullAddress());
            Long cabinetId = i.getCabinetId();
            Cabinet cabinetById = cabinetService.getCabinetById(cabinetId);
            //插槽空闲数量是否大于0，大于0可以归还
            Integer freeSlots = cabinetById.getFreeSlots();
            if(freeSlots > 0){
                stationNewVo.setIsReturn("1");
            }else {
                stationNewVo.setIsReturn("0");
            }
            //可使用充电宝数量，是否大于0，大于0可用
            Integer availableNum = cabinetById.getAvailableNum();
            if(availableNum > 0){
                stationNewVo.setIsUsable("1");
            }else {
                stationNewVo.setIsUsable("0");
            }
            return stationNewVo;
        }).collect(Collectors.toList());

        return collect1;
    }
}
